﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Product
{
    public class ProductFlagged
    {
        /// <summary>
        /// Return list of product flagged from user
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Product.ProductFlagged> GetListOfProductFlaggedFromUser(int IDUser)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR016_ListProductFlaggedFromUser", 0, objListOfParameters);

                List<LimpaGaragem.Model.Product.ProductFlagged> objListOfProductFlagged = new List<LimpaGaragem.Model.Product.ProductFlagged>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Product.ProductFlagged objProductFlagged = new LimpaGaragem.Model.Product.ProductFlagged();
                    objProductFlagged.IDUser = IDUser;
                    objProductFlagged.ID = Convert.ToInt32(objDataRow["ID_PRODUCT_FLAGGED"]);
                    objProductFlagged.IDProductFlaggedType = Convert.ToInt32(objDataRow["ID_PRODUCT_FLAGGED_TYPE"]);
                    objProductFlagged.IDProduct = Convert.ToInt32(objDataRow["ID_PRODUCT"]);
                    objProductFlagged.Content = objDataRow["DS_CONTENT"].ToString();
                    objProductFlagged.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfProductFlagged.Add(objProductFlagged);
                }
                return objListOfProductFlagged;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return list of product flagged from product
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Product.ProductFlagged> GetListOfProductFlaggedFromProduct(int IDProduct)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR016_ListProductFlaggedFromProduct", 0, objListOfParameters);

                List<LimpaGaragem.Model.Product.ProductFlagged> objListOfProductFlagged = new List<LimpaGaragem.Model.Product.ProductFlagged>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Product.ProductFlagged objProductFlagged = new LimpaGaragem.Model.Product.ProductFlagged();
                    objProductFlagged.IDProduct = IDProduct;
                    objProductFlagged.ID = Convert.ToInt32(objDataRow["ID_PRODUCT_FLAGGED"]);
                    objProductFlagged.IDProductFlaggedType = Convert.ToInt32(objDataRow["ID_PRODUCT_FLAGGED_TYPE"]);
                    objProductFlagged.IDUser = Convert.ToInt32(objDataRow["ID_USER"]);
                    objProductFlagged.Content = objDataRow["DS_CONTENT"].ToString();
                    objProductFlagged.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfProductFlagged.Add(objProductFlagged);
                }
                return objListOfProductFlagged;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Insert new product flagged
        /// </summary>
        public int InsertProductFlagged(int IDProductFlaggedType,
                                     int IDProduct,
                                     int IDUser,
                                     string Content)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT_FLAGGED_TYPE", IDProductFlaggedType));
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                objListOfParameters.Add(new SqlParameter("@DS_CONTENT", Content));

                //EXECUTE
                return objDataAccess.ExecuteQueryAndReturnValue(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR016_InsertProductFlagged", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
